<?php
/**
 * Created by PhpStorm.
 * User: UsWork-02
 * Date: 2021/3/17
 * Time: 10:51
 */

namespace app\project\controller;

use app\common\controller\Common;
use app\project\service\PublicService;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Dictionaries extends Controller
{

    //查询上级分类
    public function superiorApi(){
        try{
            $common = new Common();
            //查询一级分类
            $oneWhere['pid'] = 0;
            $oneWhere['status'] = 1;
            $oneWhere['type'] = $_POST['type'];
            $parent = $common->public_select("project_type",$oneWhere)->select();
            $black['code'] = 0;
            $black['data'] = $parent;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['data'] = $e->getMessage();
        }
        return $black;
    }


    public function _initialize()
    {
        //获取平台标题
        $thisWechat = Session::get("thisWechat");
        $platform = Db::name("config_platform")->where(['uniacid'=>$thisWechat['id']])->find();
        $this->assign("platform", $platform);
        $swidthStyle = Session::get("style");
        $this->assign("swidthStyle",$swidthStyle);
    }

    //类别列表
    public function advance(){
        //权限查询
        $common = new Common();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);
        if(!empty($_GET['keyword'])){
            $keyword = $_GET['keyword'];
            $where['name'] = ['like',"%{$keyword}%"];
        }
        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $thisWechat = Session::get("thisWechat");
                $where['uniacid'] = $thisWechat['id'];
                $where['status'] = ['gt',0];
                $list = $common->public_select("project_advance",$where,null,$start,$limit)->order("id desc")->select();
                $count = $common->public_select("project_advance",$where)->count();
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        return $this->fetch("advance");
    }


    /**
     * 详情
     */
    public function advancedetail(){
        $common = new Common();
        $thisWechat = Session::get("thisWechat");
        $id = empty($_POST['id'])?0:$_POST['id'];

        $where['id'] = $id;
        $where['uniacid'] = $thisWechat['id'];
        $detail = PublicService::detailService("project_advance",$where);
        $this->assign('detail',$detail);
//        $template = empty($_POST['id'])?"create":"update";
        return view("create");
    }

    /**
     * 添加操作
     */
    public function advanceadd(){
        $thisWechat = Session::get("thisWechat");
        try{
            $common = new Common();
            $rule = [
                ["name", "require", "类别名称不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            $data['uniacid'] = $thisWechat['id'];
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            $data['addtime'] = date("Y-m-d H:i:s",time());
            $data['add_id'] = Session::get("id");
            $id = $common->handle("project_advance",$data,1);
            $log['remark'] = "账号【" . Session::get("user_name") . "】添加项目类别【".$data['name']."】id【".$id."】";
            $common->addLog($log);

            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 修改操作
     */
    public function advancesave(){
        $thisWechat = Session::get("thisWechat");
        try{
            $common = new Common();
            $rule = [
                ["name", "require", "类别名称不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            $data['uniacid'] = $thisWechat['id'];
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            $data['updatetime'] = date("Y-m-d H:i:s",time());
            $data['update_id'] = Session::get("id");
            $common->handle("project_advance",$data,1);

            $log['remark'] = "账号【" . Session::get("user_name") . "】修改项目类别【".$data['name']."】id【".$data['id']."】";
            $common->addLog($log);

            $black['code'] = 0;
            $black['msg'] = "修改成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 删除操作
     */
    public function advancedel(){
        try{
            $common = new Common();
            $id = $_POST['id'];
            $table = $_POST['table'];
            $power = $common->public_select($table,['id'=>['in',$id]])->select();
            $res = $common->public_select($table,['id'=>['in',$id]])->delete();
            if(empty($res)){
                throw new Exception("删除失败");
            }

            foreach ($power as $k=>$v){
                $log['remark'] = "账号【" . Session::get("user_name") . "】删除项目类别【".$v['name']."】id【".$v['id']."】";
                $common->addLog($log);
            }
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    public function typelist(){
        //权限查询
        $common = new Common();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);
        if(!empty($_GET['keyword'])){
            $keyword = $_GET['keyword'];
            $where['name'] = ['like',"%{$keyword}%"];
        }

        if(!empty($_GET['list_type'])){
            $where['type'] =  $_GET['list_type'];
        }else{
            $where['type'] =  ['not in',[7,8]];
        }

        $where['pid'] = $pid = empty($_GET['pid'])?0:$_GET['pid'];
        $this->assign("pid",$pid);
        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['pageSize']) ? 10 : $_GET['pageSize'];
                $start = ($page - 1) * $limit;
                $thisWechat = Session::get("thisWechat");
                $where['uniacid'] = $thisWechat['id'];
                $where['status'] = ['gt',0];
                $list = $common->public_select("project_type",$where,null,$start,$limit)->order("sort asc,id desc")->select();
                $count = $common->public_select("project_type",$where)->count();
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        return $this->fetch("typelist");
    }


    /**
     * 详情
     */
    public function typedetail(){
        $common = new Common();
        $thisWechat = Session::get("thisWechat");
        $id = empty($_POST['id'])?0:$_POST['id'];

        //获取双千库分类
        $pro_type = config("pro_type");
        $this->assign('pro_type',$pro_type);



        if(!empty($id)){
            $where['id'] = $id;
            $where['uniacid'] = $thisWechat['id'];
            $detail = $common->public_select("project_type",$where)->find();
            $parent = [];
            if($detail['type']==4 || $detail['type']==7 || $detail['type']==8){
                //查询一级分类
                $oneWhere['pid'] = 0;
                $oneWhere['status'] = 1;
                $oneWhere['type'] = $detail['type'];
                $parent = $common->public_select("project_type",$oneWhere)->select();
            }

            if(!empty($detail['library'])){
                $detail['library'] = explode(",",$detail['library']);
            }else{
                $detail['library'] = [];
            }

        }else{
            $queryTable = Db::query("show columns from us_project_type"); //获取表结构
            foreach ($queryTable as $item) {
                $detail[$item['Field']] = "";
            };
            $detail['library'] = [];
            $parent = [];
        }

        $this->assign('parent',$parent);
        $this->assign('detail',$detail);
        $template = "update";
        return view($template);
    }

    /**
     * 添加操作
     */
    public function typeadd(){
        $thisWechat = Session::get("thisWechat");
        try{
            $common = new Common();
            $rule = [
                ["name", "require", "类别名称不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            $data['uniacid'] = $thisWechat['id'];
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            $data['addtime'] = date("Y-m-d H:i:s",time());
            $data['add_id'] = Session::get("id");

            if(!empty($data['library'])){
                $data['library'] = implode(",",$data['library']);
            }
            $id = $common->handle("project_type",$data,1);
            $log['remark'] = "账号【" . Session::get("user_name") . "】添加项目类别【".$data['name']."】id【".$id."】";
            $common->addLog($log);

            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 修改操作
     */
    public function typesave(){
        $thisWechat = Session::get("thisWechat");
        try{
            $common = new Common();
            $rule = [
                ["name", "require", "类别名称不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            $data['uniacid'] = $thisWechat['id'];
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            $data['updatetime'] = date("Y-m-d H:i:s",time());
            $data['update_id'] = Session::get("id");

            if(!empty($data['library'])){
                $data['library'] = implode(",",$data['library']);
            }else{
                $data['library'] = "";
            }

            $common->handle("project_type",$data,1);

            $log['remark'] = "账号【" . Session::get("user_name") . "】修改项目类别【".$data['name']."】id【".$data['id']."】";
            $common->addLog($log);

            $black['code'] = 0;
            $black['msg'] = "修改成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 删除操作
     */
    public function typedel(){
        try{
            $common = new Common();
            $id = $_POST['id'];
            $table = $_POST['table'];
            $power = $common->public_select($table,['id'=>['in',$id]])->select();
            $res = $common->public_select($table,['id'=>['in',$id]])->delete();

            $common->public_select($table,['pid'=>['in',$id]])->delete();
            if(empty($res)){
                throw new Exception("删除失败");
            }

            foreach ($power as $k=>$v){
                $log['remark'] = "账号【" . Session::get("user_name") . "】删除项目类别【".$v['name']."】id【".$v['id']."】";
                $common->addLog($log);
            }
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    public function index(){

        //权限查询
        $common = new Common();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);
        if(!empty($_GET['keyword'])){
            $keyword = $_GET['keyword'];
            $where['name'] = ['like',"%{$keyword}%"];
        }

        if(!empty($_GET['typeId'])){
            $typeId = $_GET['typeId'];
            $where['typeId'] = $typeId;
        }
        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $thisWechat = Session::get("thisWechat");
                $where['uniacid'] = $thisWechat['id'];
                $where['status'] = ['gt',0];

                $list = $common->public_select("project_dictionaries",$where,null,$start,$limit)->order("id desc")->select();
                $count = $common->public_select("project_dictionaries",$where)->count('*');
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }else{
            $Communal = new Communal();
            //获取产业链数据
            $IndustrialChain = $Communal->dictionary("project_type",2);
            $this->assign('IndustrialChain',$IndustrialChain);
        }
        return $this->fetch("index");
    }

    /**
     * 详情
     */
    public function detail(){
        $common = new Common();
        $thisWechat = Session::get("thisWechat");
        $id = empty($_POST['id'])?0:$_POST['id'];
        if(!empty($id)){
            $where['id'] = $id;
            $where['uniacid'] = $thisWechat['id'];
            $detail = $common->public_select("project_dictionaries",$where)->find();
            $this->assign('detail',$detail);
        }

        $Communal = new Communal();
        //获取产业链数据
        $IndustrialChain = $Communal->dictionary("project_type",2);
        $this->assign('IndustrialChain',$IndustrialChain);

        $template = empty($_POST['id'])?"addView":"saveView";
        return view($template);
    }

    /**
     * 添加操作
     */
    public function add(){
        $thisWechat = Session::get("thisWechat");
        try{
            $common = new Common();
            $rule = [
                ["name", "require", "单位名称不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            $data['uniacid'] = $thisWechat['id'];
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            $data['addtime'] = date("Y-m-d H:i:s",time());
            $data['add_id'] = Session::get("id");
            $id = $common->handle("project_dictionaries",$data,1);


            $log['remark'] = "账号【" . Session::get("user_name") . "】添加责任单位【".$data['name']."】id【".$id."】";
            $common->addLog($log);

            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 修改操作
     */
    public function save(){
        $thisWechat = Session::get("thisWechat");
        try{
            $common = new Common();
            $rule = [
                ["name", "require", "单位名称不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            $data['uniacid'] = $thisWechat['id'];
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            $data['updatetime'] = date("Y-m-d H:i:s",time());
            $data['update_id'] = Session::get("id");
            $common->handle("project_dictionaries",$data,1);

            $log['remark'] = "账号【" . Session::get("user_name") . "】修改责任单位【".$data['name']."】id【".$data['id']."】";
            $common->addLog($log);

            $black['code'] = 0;
            $black['msg'] = "修改成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 删除操作
     */
    public function del(){
        try{
            $common = new Common();
            $id = $_POST['id'];
            $table = $_POST['table'];
            $power = $common->public_select($table,['id'=>['in',$id]])->select();
            $res = $common->public_select($table,['id'=>['in',$id]])->delete();
            if(empty($res)){
                throw new Exception("删除失败");
            }
            foreach ($power as $k=>$v){
                if($table == "project_dictionaries"){
                    $log['remark'] = "账号【" . Session::get("user_name") . "】删除责任单位【".$v['name']."】id【".$v['id']."】";
                }else{
                    $log['remark'] = "账号【" . Session::get("user_name") . "】删除项目类型【".$v['name']."】id【".$v['id']."】";
                }
                $common->addLog($log);
            }
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

}